package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.fastjson.JSON
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.album.utils.Utils
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.even.MessageWrap
import com.example.library_common.utils.even.TO_REFRESH_LIST
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.CollectBean
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.SecKillLabBean
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.SecKillSubBean
import com.example.shushiyunsst.util.GOODS_DETAIL
import com.example.shushiyunsst.util.GOODS_ID
import com.example.shushiyunsst.util.startNavigation
import com.example.wallet.global.AppGlobals
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

class SecKillSubFragment : SimpleListFragment<SecKillSubBean>(), OnItemChildClickListener {

    companion object {
        fun newInstance(): SecKillSubFragment {
            val args = Bundle()
            val fragment = SecKillSubFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var labBean = SecKillLabBean()

//    private val icon = AppGlobals.getApplication().getString(R.string.sst)

    fun setLabBean(labBean: SecKillLabBean) {
        this.labBean = labBean
    }

    fun getLabBean() = labBean

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
        isEnableLoadMore = false
        isAddDividerItemDecoration = false
    }

    override fun getItemLayout() = R.layout.sec_kill_sub_fragment


    override fun initView(view: View) {
        super.initView(view)
        mAdapter.addChildClickViewIds(R.id.toBuy)
        mAdapter.setOnItemChildClickListener(this)
    }

    override fun convertItem(helper: JssBaseViewHolder?, item: SecKillSubBean?) {
        helper?.setImageNetUrl(R.id.icon, item?.goodsLogo)
            ?.setText(R.id.name, item?.goodsName)
            ?.setText(R.id.skpNumsSelled, "已抢${item?.skpNumsSelled}件")
            ?.setText(R.id.priceTv, "${SecKillSubBean.token_Type} ${item?.skpPrice}")
            ?.setText(R.id.oriPrice, "${SecKillSubBean.token_Type} ${item?.skpPriceOld}")
            ?.setProgress(R.id.progress, item?.progress() ?: 0)
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(SecKillSubBean::class.java).endSubType().build()
    }

    override fun netRequest() {
//        val jsonFromRaw = Utils.getJsonFromRaw(_mActivity, R.raw.temp_fack_data)
//        parseDate(jsonFromRaw)
//
//        val jsonFromRaw = ""
//        parseDate(jsonFromRaw)

        val map = mapOf(
            "pageCount" to 20,
            "pageIndex" to pageIndex,
            "skpTimes" to labBean.skpTimes
        )
        RestClient.builder()
            .url(R.string.querySpikeList)
            .raw(JSON.toJSONString(map))
            .success { parseDate(it) }
            .error { _, _ -> onFailed() }
            .failure { onFailed() }
            .build().post()
    }

    override fun handMessage(msg: MessageWrap?) {
        when (msg?.message) {
            TO_REFRESH_LIST -> {
                if (!mSwipeRefreshLayout.isRefreshing) {
                    mSwipeRefreshLayout.isRefreshing = true
                }
                onRefresh()
            }
        }
    }

    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val obj = JSON.parseObject(data)
        val spikeList = obj["spikeList"]?.toString() ?: ""
        val array = JSON.parseArray(spikeList, SecKillSubBean::class.java)
        loadListDate(array)
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = mAdapter.getItem(position)
        when (view.id) {
            R.id.toBuy -> {
                start(SecKillTakeOrderFragment.newInstance(item))
            }
        }
    }
}